import xlrd  # 直接使用 xlrd 1.2.0
import pandas as pd
import re

# 使用 xlrd 读取 Excel
workbook = xlrd.open_workbook('G:\PromptWord2.xlsx')
sheet = workbook.sheet_by_index(0)

# 转换为 DataFrame
data = []
for row_idx in range(sheet.nrows):
    row = sheet.row_values(row_idx)
    data.append(row)

df_all = pd.DataFrame(data[1:], columns=data[0])

# 其余代码保持不变...
def replace_color_tag(text: str) -> str:
    """处理单个单元格"""
    if pd.isna(text):
        return text
    text = str(text)

    tokens = [
        *[(m.start(), m.end(), 'open', m.group(1))
          for m in re.finditer(r'\[([0-9a-fA-F]{6})\]', text)],
        *[(m.start(), m.end(), 'close', None)
          for m in re.finditer(r'\[-\]', text)]
    ]
    tokens.sort(key=lambda t: t[0])

    out, last = [], 0
    stack = []

    for start, end, typ, val in tokens:
        out.append(text[last:start])
        last = end

        if typ == 'open':
            color = val.upper()
            out.append(f'<color=#{color}>')
            stack.append(color)
        else:
            if stack:
                out.append('</color>')
                stack.pop()

    out.append(text[last:])
    out.extend(['</color>'] * len(stack))

    return ''.join(out)

df_all['Title'] = df_all['Title'].apply(replace_color_tag)
df_all.to_excel('G:\PromptWord2.xlsx', index=False, engine='openpyxl')

print('处理完成，已保存 Reference.xlsx')